
'=======================================
' 执行所有测试 case
'=======================================
Private Sub test_PrimitiveArrayScanner_ALL_case()
    ' 基础功能测试
    test_PrimitiveArrayScanner_base
    ' toNext 测试: 每次的移动步长 = 1
    test_PrimitiveArrayScanner_toNext_step_is_1
    ' toNext 测试: 每次的移动步长 = 任意
    test_PrimitiveArrayScanner_toNext_step_is_any
    ' back 测试: 每次的移动步长 = 1
    test_PrimitiveArrayScanner_toNext_back_is_1
    ' back 测试: 每次的移动步长 = 任意
    test_PrimitiveArrayScanner_back_step_is_any
    ' ActiveIndex 测试: 活动索引测试
    test_PrimitiveArrayScanner_ActiveIndex

    ' makr 测试:
    test_PrimitiveArrayScanner_mark

    ' 测试初始化的起始索引 + 结束索引
    test_ObjArrayScanner_init_startindex
    test_ObjArrayScanner_init_endindex
End Sub

' 基础功能测试
Private Sub test_PrimitiveArrayScanner_base()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    Dim i As Long
    i = 0

    Dim v As Variant
    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend
End Sub


' toNext 测试: 每次的移动步长 = 1
Private Sub test_PrimitiveArrayScanner_toNext_step_is_1()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    Debug.Assert scanner.peek() = arrRef.Item(0)
    scanner.toNext

    Debug.Assert scanner.peek() = arrRef.Item(1)
    scanner.toNext

    Debug.Assert scanner.peek() = arrRef.Item(2)
    scanner.toNext

    Debug.Assert scanner.peek() = arrRef.Item(3)
    scanner.toNext

    Debug.Assert scanner.peek() = arrRef.Item(4)
    scanner.toNext

    Debug.Assert scanner.peek() = arrRef.Item(5)
    scanner.toNext

    Debug.Assert IsEmpty(scanner.peek())
    scanner.toNext
    Debug.Assert IsEmpty(scanner.peek())
End Sub

' toNext 测试: 每次的移动步长 = 任意
Private Sub test_PrimitiveArrayScanner_toNext_step_is_any()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    Debug.Assert scanner.peek() = arrRef.Item(0)
    scanner.toNext(2)

    Debug.Assert scanner.peek() = arrRef.Item(2)
    scanner.toNext(3)

    Debug.Assert scanner.peek() = arrRef.Item(5)

    ' 这里的内部索引将会被定位到 6
    scanner.toNext(5)
    Debug.Assert IsEmpty(scanner.peek())
    ' 这里的内部索引将会被定位到 6
    scanner.toNext
    Debug.Assert IsEmpty(scanner.peek())
End Sub

' back 测试: 每次的移动步长 = 1
Private Sub test_PrimitiveArrayScanner_toNext_back_is_1()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    ' 定位到最后
    scanner.toNext(100)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(5)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(4)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(3)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(2)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(1)

    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(0)

    ' 回退到 0 索引后，再次回退仍然是 0 索引
    scanner.back
    Debug.Assert scanner.peek() = arrRef.Item(0)
End Sub

' back 测试: 每次的移动步长 = 任意
Private Sub test_PrimitiveArrayScanner_back_step_is_any()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    ' 定位到最后 index = 6
    scanner.toNext(100)

    ' index = 6 - 2 = 4
    scanner.back(2)
    Debug.Assert scanner.peek() = arrRef.Item(4)

    ' index = 4 - 3 = 1
    scanner.back(3)
    Debug.Assert scanner.peek() = arrRef.Item(1)

    ' index = 1 - 2 = -1，这种情况下将会被自动纠正为 0
    scanner.back(2)
    Debug.Assert scanner.peek() = arrRef.Item(0)
End Sub

' ActiveIndex 测试: 活动索引测试
Private Sub test_PrimitiveArrayScanner_ActiveIndex()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    Debug.Assert scanner.ActiveIndex = 0

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 1

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 2

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 3

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 4

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 5

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 6

    scanner.toNext
    Debug.Assert scanner.ActiveIndex = 6
End Sub

' makr 测试:
Private Sub test_PrimitiveArrayScanner_mark()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim scanner As PrimitiveArrayRefScanner
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef

    ' 定位到最后 index = 6
    scanner.toNext(100)

    ' 1. reset，因为没有 mark，将会直接定位到 0
    scanner.reset
    Debug.Assert scanner.peek() = arrRef.Item(0)

    ' 2. mark 后 reset
    scanner.toNext ' -> 1
    scanner.toNext ' -> 2
    scanner.mark
    scanner.toNext ' -> 3
    scanner.toNext ' -> 4
    Debug.Assert scanner.peek() = arrRef.Item(4)
    scanner.reset ' -> 2
    Debug.Assert scanner.peek() = arrRef.Item(2)

    ' 3. 清除 mark
    scanner.toNext ' -> 3
    scanner.toNext ' -> 4
    Debug.Assert scanner.peek() = arrRef.Item(4)
    scanner.clearMark
    scanner.reset ' -> 0
    Debug.Assert scanner.peek() = arrRef.Item(0)

    ' 4. 测试 resetAndClearMark
    scanner.toNext ' -> 1
    scanner.toNext ' -> 2
    scanner.mark
    scanner.toNext ' -> 3
    scanner.toNext ' -> 4
    Debug.Assert scanner.peek() = arrRef.Item(4)
    scanner.reset ' -> 2
    scanner.toNext ' -> 3
    scanner.toNext ' -> 4
    Debug.Assert scanner.peek() = arrRef.Item(4)
    scanner.reset ' -> 2 再次 reset 后仍然是 2
    scanner.toNext ' -> 3
    scanner.toNext ' -> 4
    scanner.resetAndClearMark ' reset 后，回到 2，并清除 mark
    Debug.Assert scanner.peek() = arrRef.Item(2)
    scanner.reset ' -> 0 再次 reset 后，回因为没有 mark 而回退到 0
    Debug.Assert scanner.peek() = arrRef.Item(0)
End Sub

Private Sub test_ObjArrayScanner_init_startindex()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"

    Dim i As Long
    Dim v As String
    Dim scanner As PrimitiveArrayRefScanner


    ' 1. 起始索引 > 0
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, 1

    i = 1

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend

    ' 2. 起始索引 < 0，调整后的索引为整数
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, -2

    i = 4

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend

    ' 3. 起始索引 < 0，调整后的索引为整数
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, -100

    i = 0

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend
End Sub

' 测试初始化的结束索引
Private Sub test_ObjArrayScanner_init_endindex()
    Dim arrRef As IPrimitiveArrayRef
    Set arrRef = UtilsArrayRef.createRealPrimitiveArrayRef(5)

    arrRef.Add "k1"
    arrRef.Add "k2"
    arrRef.Add "k3"
    arrRef.Add "k4"
    arrRef.Add "k5"
    arrRef.Add "k6"


    Dim i As Long
    Dim v As String
    Dim scanner As PrimitiveArrayRefScanner


    ' 1. 结束索引 > 0
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, endIndex:= 3

    i = 0

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend
    Debug.Assert i = 4

    ' 2. 起始索引 < 0，调整后的索引为整数
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, endIndex:= -3

    i = 0

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend
    Debug.Assert i = 4

    ' 3. 结束索引 < 0，调整后的索引为整数
    Set scanner = New PrimitiveArrayRefScanner
    scanner.Init arrRef, endIndex:= -100

    i = 0

    while scanner.hasNext
        v = arrRef.Item(i)
        i = i + 1
        Debug.Assert scanner.peek() = v
        Debug.Assert scanner.getNext() = v
    Wend
    Debug.Assert i = 6
End Sub
